<?php
require_once( 'mofachi.DataConnection.php' );
abstract class ContentData {
	protected $field = array();
	protected static $queryStr;
	protected static $tableName;
	protected static $tableID;

	public function __get( $key ) {
		if( !isset( $this->field[ $key ] ) ) {
            return null;
        }
		return $this->field[ $key ];
	}
	public function __set( $key, $value ) {
		$this->field[ $key ] = $value;
	}
	protected function __construct() {
		$this->field[ static::$tableID ] = null;
		$this->field[ "user_id" ] = null;
		$this->field[ "issue_time" ] = null;
		$this->field[ "edit_time" ] = null;
		$this->field[ "vote_yes" ] = null;
		$this->field[ "vote_no" ] = null;
		$this->field[ "title" ] = null;
		$this->field[ "content" ] = null;
	}
	protected function add() {
		$db = DataConnection::getConnection();
		if( $db == null ) return false;

		if( !$db->query( static::$queryStr ) ) {
			$db->close();
			return false;
		}
		$db->close();
		return true;
	}
	protected function loadData( $row ) {
		foreach( $this->field as $key => $value ) {
			$this->field[ $key ] = $row[ $key ];
		}
	}
	public function loadByID() {
		$db = DataConnection::getConnection();
		if( $db == null ) return false;
		
		$query = "select * from ".static::$tableName." where ".static::$tableID."=".$this->field[ static::$tableID ];
		$result = $db->query( $query );
		if( !$result || $result->num_rows < 1 ) {
			$db->close();
			return false;		
		}
		$this->loadData( $result->fetch_assoc() );

		$result->free();
		$db->close();
		return true;
	}
	protected function update() {
		$db = DataConnection::getConnection();
		if( $db == null ) return false;
		
		if( !$db->query( static::$queryStr ) ) {
			$db->close();
			return false;
		}
		$db->close();
		return true;
	}
	public function vote( $yes, $no ) {
		$db = DataConnection::getConnection();
		if( $db == null ) return false;
		
		$query = "update ".static::$tableName." set vote_yes = (vote_yes+".$yes."),";
		$query.= "vote_no=(vote_no+".$no.") where ".static::$tableID."=".$this->field[ static::$tableID ];
		if( !$db->query($query) ) {
			$db->close();
			return false;
		}
		$db->close();
		return true;
	}
	public function remove() {
		$db = DataConnection::getConnection();
		if( $db == null ) return false;

		$query = "delete from ".static::$tableName." where ".static::$tableID."=".$this->field[ static::$tableID ];
		$db->query( $query );
		$db->close();
		return true;
	}
	//-------------------------------------------------------------------------------------
	// static functions
	//-------------------------------------------------------------------------------------
	public static function getRecent( $offset, $length ) {
		$db = DataConnection::getConnection();
		if( $db == null ) return null;
		
		$query = "select * from ".static::$tableName." order by edit_time desc limit ".$offset.",".$length;
		$result = $db->query( $query );
		if( !$result || $result->num_rows < 1 ) {
			$db->close();
			return null;		
		}
		$returnArray = array();
		$num = $result->num_rows;
		$className = get_called_class();
		for( $i = 0; $i < $num; $i++ ) {
			$sub = new $className();
			$sub->loadData( $result->fetch_assoc() );
			$returnArray[ $i ] = $sub;
		}
		$result->free();
		$db->close();
		return $returnArray;
	}
}
?>